Get started topic
Get Started
Install
Follow the package installation instructions
Install from git
Add to your dependencies in pubspec.yaml
handle:
git:
url: https://github.com/predatorx7/handle.git
Usage
Add handle package to your app and create your client to make an API request.
import 'package:handle/handle.dart';
// You can add deserializers to the common JsonModelSerializer so that every
// rest response can use it.
JsonModelSerializer.common.addDeserializers({
JsonDeserializerOf<TodoModel>((json) => TodoModel.fromJson(json)),
});
final jsonPlaceholderClient = RequestClient(
Client(),
url: Uri.https('jsonplaceholder.typicode.com'),
);
final todos = await jsonPlaceholderClient.get(Uri(path: '/todos')).dataAsync<List<TodoModel>>();
for (final todo in todos!) {
print(todo);
}
Alternatively, you can create a REST service too.
import 'package:handle/handle.dart';
class JsonplaceholderService extends RestService {
JsonplaceholderService()
: super(RequestClient(
Client(),
url: Uri.https('jsonplaceholder.typicode.com'),
));
Future<List<TodoModel>?> getTodos() {
return client.get(Uri(path: '/todos')).dataAsync<List<TodoModel>>();
}
}
// You can add deserializers to the common JsonModelSerializer so that every
// rest response can use it.
JsonModelSerializer.common.addDeserializers({
JsonDeserializerOf<TodoModel>((json) => TodoModel.fromJson(json)),
});
final service = JsonplaceholderService();
final todos = await service.getTodos();
for (final todo in todos!) {
print(todo);
}
For a complete sample, see the Rest Service & Request Client sample in the example directory.
For more on how to configure clients in handle
, see Configuration.
Classes
-
HttpService<
T extends Client> Get started Configuration Services - A service that uses client for making requests. It is recommend to extend this class and add your method and use the client to make requests in the methods to the server.
- JsonModelSerializer Get started Configuration
- A class that serializes and deserializes JSON objects to and from Dart classes.
- RequestClient Get started Clients
- A client that overrides url, headers of the request.
- RestClient Get started Configuration Clients
- Creates a client that returns RestResponse for a request.
- RestService Get started Configuration Services
- A service that can be used to make requests to a JSON Api. It wraps the client with RestClient if necessary to return a RestResponse. It is recommended to extend this class and add your methods to it where you use client to make requests.